package panels;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import util.Func;

public class DiceButtonPanel extends JPanel  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String CHANGED_PROPERTY = "changed";

	/**
	 * Create the panel.
	 */
	
	private ArrayList<Integer>	rolls = new ArrayList<Integer>();
	private final int dieSize;
	private JButton btnRoll;
	private JButton btnClear;
	
	
	public DiceButtonPanel(int S) {
		rolls = new ArrayList<Integer>();
		dieSize = S;

		setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		setLayout(new GridLayout(1, 0, 0, 0));
		
		btnRoll = new JButton("");
		btnRoll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int R = Func.roll(getDieSize());
				rolls.add(new Integer(R));
				update();
			}
		});
		add(btnRoll);
		
		btnClear = new JButton("");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				rolls.clear();
				update();
			}
		});
		add(btnClear);
		
		update();
	}

	synchronized public ArrayList<Integer> getRolls() {
		return rolls;
	}
	
	synchronized public int getRollsSum() {
		Iterator<Integer>	itr = rolls.iterator();
		int sum = 0;
		while(itr.hasNext()) {
			sum += itr.next().intValue();
		}
		return sum;
	}

	synchronized public int getDieSize() {
		return dieSize;
	}
	
	synchronized public int getNumDice() {
		return rolls.size();
	}
	
	synchronized public String getRollsString() {
		Iterator<Integer>	itr = rolls.iterator();
		String S = " ";
		
		while(itr.hasNext()) {
			S += itr.next().toString() + " ";
		}
		return S.substring(0, S.length()-1);
	}
	
	synchronized public String getDiceString() {
		return Integer.toString(getNumDice()) + "d" + Integer.toString(getDieSize());
	}
	
	synchronized public int getMinRoll() {
		Iterator<Integer>	itr = rolls.iterator();
		int min = getDieSize()+1;
		
		while(itr.hasNext()) {
			 min = Math.min(itr.next().intValue(), min);
		}
		return min;
	}
	
	synchronized public int getMaxRoll() {
		Iterator<Integer>	itr = rolls.iterator();
		int max = 0;
		
		while(itr.hasNext()) {
			 max = Math.max(itr.next().intValue(), max);
		}
		return max;
	}
	
	synchronized public int getLastRoll() {
		if (getNumDice()<1) {
			return 0;
		} else {
			return rolls.get(getNumDice()-1);
		}
	}
	
	synchronized public void clear() {
		rolls.clear();
		update();
	}
	
	synchronized public JButton getBtnRoll() {
		return btnRoll;
	}
	synchronized public JButton getBtnClear() {
		return btnClear;
	}
	
	synchronized public void update() {
		getBtnRoll().setText(getDiceString() + " = " + Integer.toString(getRollsSum()));
		getBtnClear().setText(getRollsString());

		this.firePropertyChange(CHANGED_PROPERTY, false, true);
		invalidate();
	}
	
	@Override
	public void paint(Graphics G) {
		super.paint(G);
	}
	
}
